package com.itheima.dubbo.web;

import com.itheima.dubbo.User;
import com.itheima.dubbo.UserFace;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {
    // @DubboReference  //远程服务调用
    //
    // @DubboReference (timeout = 2000) //远程服务调用 超时时间
    /*@DubboReference(methods = {
            @Method(name = "getById",
                    timeout = 1500)
    })  //标识服务消费方的注解 超时时间 在方法上*/
   //  @DubboReference(retries = 2) 消费端设置重试次数
   // @DubboReference(version = "1.0.1") // 灰度发布-版本号
    @DubboReference(check = false) //启动时设置不检查服务提供方是否存在
  //  @DubboReference(cluster = "failfast") // 容错机制failfast 快速失败值,调用一次
  //  @DubboReference(loadbalance = "roundrobin")  //负载均衡策略修改 roundrobin: 轮询
    UserFace userFace;

    @GetMapping("hello")
    public String hello(String username) {
        return userFace.hello(username);
    }

    @GetMapping("getById")
    public User getById(Long id) {


        // 隐式传参，后面的远程调用都会隐式将这些参数发送到服务器端，类似cookie，用于框架集成，不建议常规业务使用
        RpcContext.getContext().setAttachment("index", "1");

        return userFace.getById(new User(id, null, null));
    }
}
